package com.lcredu.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 黎长容
 * @date 2025-09-05 14:45
 */
@Configuration
public class RabbitMQConfig {

    private static final String QUEUE = "queue";


    /**
     * 1.创建两个广播队列
     * 2.创建一个交换机
     * 3.队列绑定交换机
     */

    //交换机--fanout------------
    private static final String FANOUTEXCHANGE = "fanoutExchange";
    private static final String QUEUE1 = "queue_fanout01";
    private static final String QUEUE2 = "queue_fanout02";
    //交换机--fanout------------


    //交换机--direct------------
    public static final String QUEUE_DIRECT01 = "queue_direct01";
    public static final String QUEUE_DIRECT02 = "queue_direct02";

    public static final String ROUTER_KEY01 = "success";
    public static final String ROUTER_KEY02 = "error";

    public static final String DIRECT_EXCHANGE = "directExchange";

    //交换机--direct------------

    @Bean
    public Binding bindingDirect01() {
        return BindingBuilder.bind(queueDirect01()).to(directExchange()).with(ROUTER_KEY01);
    }

    @Bean
    public Binding bindingDirect02() {
        return BindingBuilder.bind(queueDirect02()).to(directExchange()).with(ROUTER_KEY02);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue queueDirect01() {
        return new Queue(QUEUE_DIRECT01);
    }

    @Bean
    public Queue queueDirect02() {
        return new Queue(QUEUE_DIRECT02);
    }

    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(FANOUTEXCHANGE);
    }

    @Bean
    public Binding binding01() {
        return BindingBuilder.bind(queue1()).to(exchange());
    }

    @Bean
    public Binding binding02() {
        return BindingBuilder.bind(queue2()).to(exchange());
    }

    @Bean
    public Queue queue1() {
        return new Queue(QUEUE1);
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE2);
    }

    @Bean
    public Queue queue() {
        return new Queue(QUEUE, true);
    }
}
